 //
//  AppUpdateViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/9/20.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "AppUpdateViewController.h"
#import "ZJAppUpdateAlertView.h"
#import "LCustomAlertView.h"
#import <AFNetworking.h>
@interface AppUpdateViewController ()<ZJAppUpdateAlertViewDelegate , LCustomAlertDelegate>
{
    NSDictionary *_appUpdateInfo;
    JCAlertView * alertView;
}
/**    */
@property (strong, nonatomic) JCAlertView  *jcAlertView;
/** imageView */
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation AppUpdateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.imageView];
    [self netWorking_appUpdate];
    
}

#pragma mark - 代理消失改页面
- (void)delegate_show{
    if (self.delegate) {
        [self.delegate AppUpdateViewControllerDeleGate_show];
    }
}
#pragma mark - 软件更新
- (void)netWorking_appUpdate{
    __weak typeof(self) weakSelf = self;
    
    [YBRequestManager request_upDateInfoWithSuccessBlock:^(id object) {
        NSLog(@"%@" , object);
        if ([[object objectForKey:@"STATE"] isEqualToString:@"20000"]) {
            [weakSelf appUpdate_Dict:object];
        }else{
            [weakSelf delegate_show];
        }
    } failBlock:^(id object) {
        [weakSelf delegate_show];
    }];
}




///版本对比
- (void)appUpdate_Dict:(NSDictionary *)dict{
    _appUpdateInfo = dict;
    //获取应用程序版本号
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
    //网上版本号
    NSString *newVersionCode = [dict objectForKey:@"VERSIONCODE"];
    if ([newVersionCode floatValue] > [currentVersion floatValue]) {//更新
        ZJAppUpdateAlertView *view = [[ZJAppUpdateAlertView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        view.dict = dict;
        view.delegate = self;
        alertView = [[JCAlertView alloc] initWithCustomView:view dismissWhenTouchedBackground:YES];
        [alertView show];
    }else{//没有更新
        [self delegate_show];
    }
}

- (void)ZJAppUpdateAlertViewDelegate_canleClick{
    [alertView dismissWithCompletion:^{
        [self delegate_show];
    }];
}

- (void)ZJAppUpdateAlertViewDelegate_updateClick{
    [alertView dismissWithCompletion:nil];
    
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
            [self delegate_show];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[_appUpdateInfo objectForKey:@"PATH"]]];
        }
        else{
            LCustomAlertView *alert = [[LCustomAlertView alloc] initWithFrame:self.view.bounds alertMessage:@"当前不在WiFi环境下，是否更新" titlesArray:@[@"否",@"是"]];
            alert.delegate = self;
            self.jcAlertView = [[JCAlertView alloc]initWithCustomView:alert dismissWhenTouchedBackground:YES];
            [self.jcAlertView show];
        }
    }];
    [mgr startMonitoring];
}


- (void)lCustomAlertWithView:(LCustomAlertView *)view index:(NSInteger)index{
    [self.jcAlertView dismissWithCompletion:nil];
    if (index == 1) {
        [self delegate_show];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[_appUpdateInfo objectForKey:@"PATH"]]];
    }
    else{
        [self delegate_show];
    }
}


- (UIImageView *)imageView{
    if (_imageView == nil){
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight])];
        _imageView.image = [UIImage imageNamed:@"welcomepage"];
    }
    return _imageView;
}

@end
